package com.bj.zspace.bytebuddy.bbtest;

import com.bj.zspace.bytebuddy.normal_test.Service;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;

import java.io.File;
import java.lang.reflect.Modifier;

/**
 * 新增方法
 **/
public class NewMethodClass {

    public static void main(String[] args) throws Exception {
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        DynamicType.Unloaded<Service> unloaded = new ByteBuddy()
            .redefine(Service.class)
            .name("a.b.SubService")
            .defineMethod("selectUser2", String.class, Modifier.PUBLIC + Modifier.STATIC)
            .withParameter(String[].class, "args")
            .intercept(FixedValue.value("new method"))
            .make();

        unloaded.saveIn(new File(path));

        /*Class<? extends Service> loaded = unloaded.load(InstanceMethodClass.class.getClassLoader()).getLoaded();
        Service service = (Service)loaded.getDeclaredConstructors()[0].newInstance();
        System.out.println(service.toString());*/


    }

}
